package 剑指offer;

import 链表.ListNode;

public class _25合并两个顺序链表 {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode p = l1, q= l2;
        ListNode head = new ListNode(0);
        ListNode h = head;
        while (q != null && p != null){
            if (p.val < q.val){
                h.next = new ListNode(p.val);
                p = p.next;
            }else{
                h.next = new ListNode(q.val);
                q = q.next;
            }
            h = h.next;
        }
        if(p != null){
            h.next = new ListNode(p.val);
            h = h.next;
            p = p.next;
        }
        if(q != null){
            h.next = new ListNode(q.val);
            h = h.next;
            q = q.next;
        }
        return head.next;
    }
}
